//
//  NewViewController.m
//  SunXi
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "NewViewController.h"

@interface NewViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong) NSMutableArray *mutableArr;
@property(nonatomic, assign) NSInteger number;
@property(nonatomic, strong) NSArray *AllArr;
@end

@implementation NewViewController{
    UIView *_nilview;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"新消息" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"yonghu1"] style:UIBarButtonItemStylePlain target:self action:@selector(presentRightMenuViewController:)];
    [self.navigationItem.rightBarButtonItem setTintColor:BUTTONCOLOR];
    self.navigationItem.title = @"消息列表";
    SV.BC = [UIColor whiteColor];

    self.manager = [CoreDataManager defaults];
    self.number = 0;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createData) name:@"新消息" object:nil];
    [self initMyTableView];
    [self initnil];
}
- (void)initMyTableView{
    self.tableView = ({
        UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        table.delegate = self;
        table.dataSource = self;
        table.rowHeight = 100;
        table;
    });
    [SV addSubview:self.tableView];
    
}
/**
 *  请求列表
 */
- (void)createData{
    self.mutableArr = [NSMutableArray array];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"UserMess"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"time" ascending:NO];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error;
    NSArray *array = [[self.manager managedObjectContext] executeFetchRequest:request error:&error];
    self.AllArr = array;
    for (UserMess *temp in array) {
        NSInteger q = self.mutableArr.count;
        BOOL jia = YES;
        for (NSInteger i = 0; i < q; i++) {
            UserMess *mutemp = self.mutableArr[i];
           if ([temp.user isEqualToString:mutemp.user]) {
               jia = NO;
               q = i;
            }
        }
        if (jia) {
            [self.mutableArr addObject:temp];;
        }
        if (self.mutableArr.count == 0) {
            [self.mutableArr addObject:temp];
        }
    }

    [self.tableView reloadData];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self createData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.mutableArr.count < 1) {
        _nilview.hidden = NO;
    }else{
        _nilview.hidden = YES;
    }
    
    
    return self.mutableArr.count;
}
- (void)initnil{
    if (!_nilview) {
        _nilview = [[UIView alloc]initWithFrame:self.tableView.frame];
        _nilview.backgroundColor = [UIColor whiteColor];
        [SV addSubview:_nilview];
        UIImageView *aimageView = [[UIImageView alloc]init];
        [_nilview addSubview:aimageView];
        [aimageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(100, 100));
            make.center.equalTo(_nilview);
        }];
        
        aimageView.image = [UIImage imageNamed:@"hezi"];
    }
    UILabel *label = [[UILabel alloc]init];
    [_nilview addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.left.equalTo(_nilview);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(_nilview).offset(-120);
    }];
    label.textAlignment = 1;
    label.text = @"收藏空空";
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myfriendCell"];
    if (!cell) {
        cell = [[NewHomeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"myfriendCell"];
    }
    
    
    cell.usermodel = self.mutableArr[indexPath.row];
    NSInteger i = 0;
    for (UserMess *temp in self.AllArr) {
        if ([[self.mutableArr[indexPath.row] user] isEqualToString:temp.user] && [temp.look isEqualToNumber:@1]) {
            i++;
        }
    }

    cell.number = [NSNumber numberWithInteger:i];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewDataiewController *DataVC = [[NewDataiewController alloc]init];
    
    DataVC.usermane = [self.mutableArr[indexPath.row] user];
    [self.navigationController pushViewController:DataVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
